package com.silverbox.sweeporder.common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.silverbox.shopper.entity.AlipayApplyAuthToken;

/**
 * 刷新令牌
 * 
 * @author Jiangyougan
 *
 */
public class RefreshToken {
	public static Map<String, Object> execute(AlipayApplyAuthToken app) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		if (app != null) {
			if(app.getRefreshToken()!=null){
				Map<String, String> map = OpenAuthTokenApp.execute(
						app.getAppId(), "refresh_token",
						app.getRefreshToken());
				if (map.get("success").equals("true")) {
					Map<String, Object> map1 = OpenAuthTokenAppQuery.execute(
							app.getAppId(), map.get("authToken"));
					if (map1.get("success").equals("true")) {
						app.setAuthToken(map.get("authToken"));
						app.setRefreshToken(map.get("refreshToken"));
						app.setExpiresIn(map.get("expiresIn"));
						app.setReExpiresIn(map.get("reExpiresIn"));
						app.setAuthStart((Date)(map1.get("authStart")));
						app.setAuthEnd((Date)map1.get("authEnd"));
						app.setStatus(map1.get("status").toString());
						returnMap.put("success", "true");
						returnMap.put("data", app);
					} else {
						returnMap.put("success", "false");
						returnMap.put("msg", map1.get("msg"));
					}
				} else {
					returnMap.put("success", "false");
					returnMap.put("msg", map.get("msg"));
				}
			} else {
				returnMap.put("success", "false");
				returnMap.put("msg", "刷新令牌不存在，请重新授权！");
			}
		} else {
			returnMap.put("success", "false");
			returnMap.put("msg", "授权信息不存在，请重新授权！");
		}
		return returnMap;
	}

}
